<?php
/**
 * Created by PhpStorm.
 * User: lullabies
 * Date: 2019/5/26
 * Time: 14:51
 */

namespace app\api\controller;
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,fs-token");
header('Access-Control-Allow-Methods: GET, POST');
use app\api\model\Brand as BrandModel;

/**
 * @title 商品品牌接口
 * @group app
 */
class Brand extends  Base
{
    /**
     * @title 获取品牌列表
     * @author 开发者
     * @url /api/brand/page
     * @method POST/GET
     * @param name:page require:0 default:1 type:int desc:当前页面数  默认第一页
     * @param name:page_size require:0 default:10 type:int desc:每页显示记录数 默认10条
     * @param name:key require:0 default:'' type:varchar desc:搜索参数
     * @param name:sort require:0 default:update_time type:vatchar desc:排序关键字 默认更新时间
     * @param name:order require:0 default:desc type:varchar desc:默认排序为降序
     * @return  id:品牌id
     * @return  name:品牌名称
     * @return image:品牌图片
     * @return letter:品牌首字母
     * @return update_time:更新时间
     */
    public function page(){
        //当前页数
        $page = input('page', 1, 'intval');
        //让前端设置数据一页显示几条数据
        $page_size = input('page_size', 10, 'intval');
        //搜索关键字
        $key = input('key','');
        //排序的关键字
        $sort = input('sort','update_time');
        //显示顺序
        $order = input('order','desc');
        //获取品牌总数
        $total = BrandModel::where('status',1)->count();
        //查询结果
        $result = BrandModel::whereLike('name',"%{$key}%")
                ->where('status',1)
                ->order($sort,$order)
                ->page($page,$page_size)
                ->select();
        if($result->isEmpty()){
            $this->apiReturn(303,'为搜索到相关信息！');
        }else{
            $this->apiReturn(200,'查询成功！',$result,$total);
        }
    }
}